﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Practices.Unity;
using CoderJoe.WoW.Proxies.Armory;
using CoderJoe.WoW.Services;
using System.Threading;

namespace CoderJoe.WoW.GearManager
{
    internal class CharacterController
        : Controller
    {
        public CharacterController(Workspace workspace)
            : base(workspace)
        {
        }

		internal void Load(string realm, string name)
		{
			CharacterModel model = Workspace.Resolve<CharacterModel>();
			WoWArmoryProxy proxy = Workspace.Resolve<WoWArmoryProxy>();

			Character c = proxy.GetCharacter(realm, name);
			model.LoadModel(c);

			//MessageBox.Show(String.Format("Realm: {0}\r\nName: {1}", realm, name));
            foreach (CharacterItemModel itemModel in model.Items)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(LoadImage), itemModel);
            }
		}

        private void LoadImage(object state)
        {
            CharacterItemModel itemModel = (CharacterItemModel)state;
            if (itemModel.Icon == String.Empty)
            {
                itemModel.Image = null;
            }
            else
            {
                ItemService service = Workspace.Resolve<ItemService>();
                itemModel.Image = service.GetItemImage(itemModel.Icon);
            }
        }
	}
}
